const { INTEGER, STRING, TINYINT, DATE, Model } = require('sequelize');
const db = require('../../helpers/db');
const GoodsCategory = require('./goods-category');

class Category extends Model {}

Category.init(
    {
        id: {
            type: INTEGER,
            autoIncrement: true,
            primaryKey: true,
            comment: '分类ID'
        },
        name: {
            type: STRING(128),
            allowNull: false,
            comment: '分类名'
        },
        cover: {
            type: STRING(128),
            allowNull: false,
            comment: '默认封面图'
        },
        parent: {
            type: STRING(32),
            comment: '外键，父节点id'
        },
        path: {
            type: STRING(128),
            allowNull: false,
            comment: '节点路径（1,2,3,5）'
        }
    },
    {
        tableName: 'categories',
        underscored: true,
        sequelize: db
    }
);

Category.hasMany(Category, {
    foreignKey: 'parent'
});

Category.hasMany(GoodsCategory, {
    foreignKey: 'category'
});

module.exports = Category;
